<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\RefundGoods;
use Auth;
use App\Interfaces\RefundGoodsService;
use App\Commands\OperationLogCommand;

class RefundGoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $search = ['order_sn'=>'', 'refund_sn'=>'', 'goods_sn'=>'', 'refund_status'=>'', 'real_name'=>'', 'phone'=>''];
        $refundStatusArr = ['0'=>'选择状态', '1'=>'待确认', '2'=>'已受理', '3'=>'处理完毕', '4'=>'不予受理', '4'=>'已取消'];

        $query = RefundGoods::orderBy('refund_id', 'desc');

        if (!empty($request->get('order_sn'))){
            $query = $query->where('order_sn', 'like', '%'.trim($request->get('order_sn')).'%');
            $search['order_sn'] = $request->order_sn;
        }//end if()

        if (!empty($request->get('refund_sn'))){
            $query = $query->where('refund_sn', 'like', '%'.trim($request->get('refund_sn')).'%');
            $search['refund_sn'] = $request->refund_sn;
        }//end if()

        if (!empty($request->get('goods_sn'))){
            $query = $query->where('goods_sn', 'like', '%'.trim($request->get('goods_sn')).'%');
            $search['goods_sn'] = $request->goods_sn;
        }//end if()

        if (!empty($request->get('refund_status')) && $request->get('refund_status')!=0){
            $query = $query->where('refund_status', trim($request->get('refund_status')));
            $search['refund_status'] = $request->refund_status;
        }//end if()

        if (!empty($request->get('real_name'))){
            $query = $query->where('real_name', 'like', '%'.trim($request->get('real_name')).'%');
            $search['real_name'] = $request->real_name;
        }//end if()

        if (!empty($request->get('phone'))){
            $query = $query->where('phone', 'like', '%'.trim($request->get('phone')).'%');
            $search['phone'] = $request->phone;
        }//end if()
        $refundGoods = $query->paginate(10);

        return view('admin.customer.index', compact(['refundGoods', 'search', 'refundStatusArr']));

    }//end func index()

    /**
     * 创建收货服务单
     * @param string $refund_sn         退货编号
     * @param string $order_sn          订单编号
     * @param int $goods_id             商品id
     * @param int $order_goods_id       订单商品表id
     * @param string $goods_sn          商品编号
     * @param mediumint $number         商品数量
     * @param tinyint $refund_status    售后服务状态
     * @param int $user_id              申请售后的用户id
     * @param string $real_name         用户真实姓名（从前台得到的）
     * @param string $phone             用户的电话号码，与购买时所用的号码一致
     * @param decimal $shop_price       商品单价
     * @param string $reason            退货原因
     * @param string $remark            售后备注信息
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $refund_sn = '';
        $order_sn = '';
        $goods_id = '';
        $order_goods_id = '';
        $goods_sn = '';
        $number = '';
        $refund_status = '';
        $user_id = '';
        $real_name = '';
        $phone = '';
        $shop_price = '';
        $reason = '';
        $remark = '';

    }//end func create()

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }//end func store()

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }//end func show()

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }//end func edit()

    /**
     * 售后单状态修改（受理、不予受理、处理完毕）
     * 1:待确认; 2:已受理；3:处理完毕；4:不予受理；5:已取消；
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $refund_sn, RefundGoodsService $refundGoodsService)
    {
        $operation = $request->operation;

        switch(trim($operation)){
            case 'yes':
                $refund_status = 2;
                break;

            case 'no':
                $refund_status = 4;
                break;

            case 'ok':
                $refund_status = 3;
                break;
        }//end switch()

        $user_id = Auth::user()->id;
        $refundGoodsService->updateReturnGoods($user_id, $refund_sn, $refund_status, '', '', '');


        switch(trim($operation)){
            case 'yes':
                $this->dispatch(
                    new OperationLogCommand('确认受理售后申请', 'refund_goods','refund_sn:'.$refund_sn)
                );
                break;

            case 'no':
                $this->dispatch(
                    new OperationLogCommand('不予受理售后申请', 'refund_goods','refund_sn:'.$refund_sn)
                );
                break;

            case 'ok':
                $this->dispatch(
                    new OperationLogCommand('售后处理完毕', 'refund_goods','refund_sn:'.$refund_sn)
                );

                break;
        }//end switch()

        $pre_url = @$request->pre_url;

        if($pre_url){

            return Redirect($pre_url);
        }else{

            return Redirect('/admin/refund');
        }
    }//end func update()

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }//end func destroy()
}
